﻿Public Class DialogItem
    Public execValue As String
    Private Sub MbGlassButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MbGlassButton3.Click
        Me.Close()
    End Sub
    Dim dtgroup As DataTable

    Sub ExecGroup(ByVal opt As String, ByVal vgroupid As Integer, ByVal vgroupname As String)
        dtgroup = New DataTable
        If Utils.executeSP("proc_itemgroup", New Object() {opt, vgroupid, vgroupname}, dtgroup) Then


        End If
    End Sub
    Sub RefreshGridGroup()
        ExecGroup("selectlike", 0, TxtNamaGroup.Text)
        If dtgroup IsNot Nothing Then
            DGVgroup.DataSource = dtgroup
        End If
    End Sub
     
    Private Sub DialogItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If execValue = "group" Then
            TxtNamaGroup.Text = ""
            BtnRefresh.PerformClick()
            TxtNamaGroup.Enabled = False
            DGVgroup.Enabled = True
            RefreshGridGroup()
            TabControl1.TabPages.RemoveAt(1)
        ElseIf execValue = "item" Then
            TabControl1.TabPages.RemoveAt(0)
            setcombocari()
            searchitem("all")
            TabControl2.TabPages.Item(0).Select()
            PnlBtn.Visible = False
        End If
    End Sub

    Private Sub TxtNamaGroup_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNamaGroup.TextChanged
        If CbxCari.Checked Then
            RefreshGridGroup()
        End If
    End Sub

    Private Sub btnAddPetugas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddPetugas.Click
        
        If btnAddPetugas.Text = "Add" Then
            txtGroupItem.Text = ""
            TxtNamaGroup.Text = ""
            TxtNamaGroup.Enabled = True
            CbxCari.Checked = False
            DGVgroup.Enabled = False
            btnDelPetugas.Enabled = False
            btnEditPetugas.Enabled = False
            btnAddPetugas.Text = "Save"
        ElseIf btnAddPetugas.Text = "Save" Then
            If TxtNamaGroup.Text = String.Empty Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    TxtNamaGroup.Text = ""
                    TxtNamaGroup.Enabled = True
                    CbxCari.Checked = False
                    DGVgroup.Enabled = False
                    btnDelPetugas.Enabled = False
                    btnEditPetugas.Enabled = False
                    btnAddPetugas.Text = "Save"
                    TxtNamaGroup.Focus()
                    Exit Sub
                Else
                    TxtNamaGroup.Text = ""
                    TxtNamaGroup.Enabled = False
                    CbxCari.Checked = False
                    btnDelPetugas.Enabled = True
                    btnEditPetugas.Enabled = True
                    DGVgroup.Enabled = True
                    btnAddPetugas.Text = "Add"
                    Exit Sub
                End If
            End If

            ExecGroup("insert", 0, TxtNamaGroup.Text)
            If dtgroup IsNot Nothing Then
                If dtgroup.Rows.Count > 0 Then
                    MsgBox(dtgroup(0)(0))
                End If
            End If
            BtnRefresh.PerformClick()
        End If
    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        txtGroupItem.Text = ""
        TxtNamaGroup.Text = ""
        TxtNamaGroup.Enabled = False
        CbxCari.Checked = False
        RefreshGridGroup()
        btnEditPetugas.Text = "Edit"
        'btnDelPetugas.Text = ""
        btnAddPetugas.Text = "Add"
        btnEditPetugas.Enabled = True
        btnDelPetugas.Enabled = True
        btnAddPetugas.Enabled = True
        TxtNamaGroup.Enabled = False
        DGVgroup.Enabled = True
    End Sub

    Private Sub CbxCari_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CbxCari.CheckedChanged
        If CbxCari.Checked Then
            TxtNamaGroup.Enabled = True
        Else
            TxtNamaGroup.Enabled = False
        End If
    End Sub

    Private Sub DGVgroup_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVgroup.CellClick
        If e.RowIndex = -1 Then
            Exit Sub
        Else
            txtGroupItem.Text = DGVgroup(txtGroupItem.Tag, e.RowIndex).value
            TxtNamaGroup.Text = DGVgroup(TxtNamaGroup.Tag, e.RowIndex).value
        End If
    End Sub

    Private Sub btnEditPetugas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditPetugas.Click
        If TxtNamaGroup.Text = String.Empty And txtGroupItem.Text = String.Empty Then
            MessageBox.Show("Silahkan Pilih Data Pada Grid/List !!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End If
        If btnEditPetugas.Text = "Edit" Then
            'txtGroupItem.Text = ""
            'TxtNamaGroup.Text = ""
            TxtNamaGroup.Enabled = True
            CbxCari.Checked = False
            DGVgroup.Enabled = False
            btnDelPetugas.Enabled = False
            btnAddPetugas.Enabled = False
            btnEditPetugas.Text = "Save"
        ElseIf btnEditPetugas.Text = "Save" Then
            If TxtNamaGroup.Text = String.Empty And txtGroupItem.Text = String.Empty Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    'TxtNamaGroup.Text = ""
                    TxtNamaGroup.Enabled = True
                    CbxCari.Checked = False
                    DGVgroup.Enabled = False
                    btnDelPetugas.Enabled = False
                    btnAddPetugas.Enabled = False
                    btnEditPetugas.Text = "Save"
                    TxtNamaGroup.Focus()
                    Exit Sub
                Else
                    TxtNamaGroup.Text = ""
                    TxtNamaGroup.Enabled = False
                    CbxCari.Checked = False
                    btnDelPetugas.Enabled = True
                    btnAddPetugas.Enabled = True
                    DGVgroup.Enabled = True
                    btnEditPetugas.Text = "Edit"
                    Exit Sub
                End If
            End If

            ExecGroup("update", CInt(txtGroupItem.Text), TxtNamaGroup.Text)
            If dtgroup IsNot Nothing Then
                If dtgroup.Rows.Count > 0 Then
                    MsgBox(dtgroup(0)(0))
                End If
            End If
            BtnRefresh.PerformClick()
        End If
    End Sub

    Private Sub btnDelPetugas_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelPetugas.Click
        If TxtNamaGroup.Text = String.Empty And txtGroupItem.Text = String.Empty Then
            MessageBox.Show("Silahkan Pilih Data Pada Grid/List !!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        Else

            If MessageBox.Show("Apakah Anda Yakin Akan Menghapus Data ? ", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                ExecGroup("delete", CInt(txtGroupItem.Text), TxtNamaGroup.Text)
                If dtgroup IsNot Nothing Then
                    If dtgroup.Rows.Count > 0 Then
                        MsgBox(dtgroup(0)(0))
                        BtnRefresh.PerformClick()
                    End If
                End If
            End If
        End If
    End Sub
    'item
    Sub setcombocari()
        Dim dt As New DataTable
        ComCariBy.Items.Clear()
        If Utils.executeSP("proc_showallcolumn", New Object() {"vitem"}, dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    For Each dr As DataRow In dt.Rows
                        ComCariBy.Items.Add(dr("Field"))
                    Next
                End If
            End If
        End If
    End Sub

    Sub searchitem(ByVal finder As String)
        Dim dtitem As New DataTable
        Dim obj() As Object
        If finder = "all" Then
            obj = New Object() {"*", "vitem", "namaitem", ""}
        Else
            obj = New Object() {"*", "vitem", ComCariBy.Text, txtKunci.Text}
        End If
        If Utils.executeSP("proc_searchall", obj, dtitem) Then
            If dtitem IsNot Nothing Then
                DGVitem.DataSource = dtitem
            End If
        End If
    End Sub
    Private Sub ComCariBy_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComCariBy.SelectedIndexChanged

    End Sub

    Private Sub btnCloseItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseItem.Click
        Me.Close()
    End Sub

    Private Sub TabControl2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl2.SelectedIndexChanged
        If TabControl2.SelectedIndex = 1 Then
            PnlBtn.Visible = True
            Dim dt As New DataTable
            vsatuanbeli.Items.Clear()
            vsatuanjual.Items.Clear()

            If Utils.executeSP("proc_satuan", New Object() {"select", Convert.DBNull}, dt) Then
                If dt IsNot Nothing Then
                    If dt.Rows.Count > 0 Then
                        For Each dr As DataRow In dt.Rows
                            vsatuanbeli.Items.Add(dr("satuan"))
                            vsatuanjual.Items.Add(dr("satuan"))
                        Next
                    End If
                End If
            End If
            dt = New DataTable
            vgroupitem.DataSource = Nothing
            If Utils.executeSP("proc_itemgroup", New Object() {"select", Convert.DBNull, Convert.DBNull}, dt) Then
                If dt IsNot Nothing Then
                    If dt.Rows.Count > 0 Then
                        vgroupitem.DataSource = dt
                        vgroupitem.DisplayMember = "namagroupitem"
                        vgroupitem.ValueMember = "groupitem"
                    End If
                End If
            End If
            setentry(False)
        Else
            PnlBtn.Visible = False
        End If
    End Sub

    Private Sub txtKunci_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKunci.TextChanged
        If Not ComCariBy.Text = "" Then
            searchitem(txtKunci.Text)
        End If
    End Sub

    Private Sub DGVitem_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVitem.CellClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If

        If TabControl2.SelectedIndex = 1 Then
            For Each ctl As Control In TabControl2.TabPages(1).Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Text = IIf(IsDBNull(DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    If cb.DataSource Is Nothing Then
                        cb.Text = IIf(IsDBNull(DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value), "", DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                    Else
                        cb.SelectedValue = IIf(IsDBNull(DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                    End If

                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Checked = IIf(IsDBNull(DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value), 0, DGVitem(ctl.Name.Replace("v", ""), e.RowIndex).Value)
                End If
            Next
        End If
    End Sub

    Sub setentry(ByVal active As Boolean)
        If active Then
            For Each ctl As Control In TabControl2.TabPages(1).Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Enabled = True
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    cb.Enabled = True
                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Enabled = True
                End If
            Next
            DGVgroup.Enabled = False
        Else
            For Each ctl As Control In TabControl2.TabPages(1).Controls
                If TypeOf ctl Is TextBox Then
                    ctl.Text = ""
                    ctl.Enabled = False
                ElseIf TypeOf ctl Is ComboBox Then
                    Dim cb As ComboBox
                    cb = CType(ctl, ComboBox)
                    cb.Enabled = False
                ElseIf TypeOf ctl Is CheckBox Then
                    Dim cbx As CheckBox
                    cbx = CType(ctl, CheckBox)
                    cbx.Checked = False
                    cbx.Enabled = False
                End If
            Next
            DGVgroup.Enabled = True
        End If
    End Sub
    Sub setbutton(ByVal execname As String, ByVal active As Boolean)
        Dim btnname() As String
        btnname = New String() {"Add", "Edit", "Delete"}
       
        For Each ctl As Control In PnlBtn.Controls
            If TypeOf ctl Is Button Then
                If ctl.Tag = btnname(0) Or ctl.Tag = btnname(1) Or ctl.Tag = btnname(2) Then
                    If active Then
                        ctl.Enabled = False
                    Else
                        ctl.Enabled = True
                    End If
                    If ctl.Tag = execname Then
                        If active Then
                            ctl.Enabled = True
                            ctl.Text = "Save"
                        Else
                            ctl.Text = ctl.Tag
                        End If
                    Else
                        ctl.Text = ctl.Tag
                    End If
                End If
            End If
        Next

    End Sub
    Private Sub btnRefreshItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefreshItem.Click
        For Each ctl As Control In TabControl2.TabPages(1).Controls
            If TypeOf ctl Is TextBox Then
                If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                    Dim cnum As AMS.TextBox.NumericTextBox
                    cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                    cnum.Text = 0
                Else
                    ctl.Text = ""
                End If
                ctl.Enabled = False
            ElseIf TypeOf ctl Is ComboBox Then
                Dim cb As ComboBox
                cb = CType(ctl, ComboBox)
                cb.Enabled = False
            ElseIf TypeOf ctl Is CheckBox Then
                Dim cbx As CheckBox
                cbx = CType(ctl, CheckBox)
                cbx.Checked = False
                cbx.Enabled = False
            End If
        Next
        searchitem("all")
        setbutton("", False)
        DGVgroup.Enabled = True
    End Sub
    Function setparamitem(ByVal exec As String) As Object()
        Dim param As New List(Of Object)
        Dim obj() As Object = New Object() {"viditem", "vnamaitem", "vgroupitem", "vhargabeli", "vqtybeli", "vsatuanbeli", "vqtyjual", "vsatuanjual", "vhargajual", "vbatasminim", "vketerangan", "vpersediaan"}
        param.Clear()
        param.Add(exec)
        For Each s As String In obj
            For Each ctl As Control In TabControl2.TabPages(1).Controls
                If ctl.Name = s Then
                    
                    If TypeOf ctl Is TextBox Then
                        If TypeOf ctl Is AMS.TextBox.NumericTextBox Then
                            Dim cnum As AMS.TextBox.NumericTextBox
                            cnum = CType(ctl, AMS.TextBox.NumericTextBox)
                            If cnum.Text = "" Then cnum.Text = 0
                            param.Add(CDbl(cnum.NumericText))
                        Else
                            param.Add(IIf(ctl.Text = "", Convert.DBNull, ctl.Text))
                        End If
                    ElseIf TypeOf ctl Is ComboBox Then
                        Dim cbx As ComboBox
                        cbx = CType(ctl, ComboBox)
                        If cbx.DataSource IsNot Nothing Then
                            param.Add(cbx.SelectedValue)
                        Else
                            param.Add(cbx.Text)
                        End If
                    ElseIf TypeOf ctl Is CheckBox Then
                        Dim cbx As CheckBox
                        cbx = CType(ctl, CheckBox)

                        param.Add(IIf(cbx.Checked, 1, 0))
                    End If
                End If
            Next
        Next
        Return param.ToArray
    End Function
    Sub ExecItem(ByVal execname As String)
        Dim dt As New DataTable
        If Utils.executeSP("proc_item", setparamitem(execname), dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    MsgBox(dt(0)(0))
                End If
            End If

        End If
    End Sub
    Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
        If btnAddItem.Text = btnAddItem.Tag Then
            btnRefreshItem.PerformClick()
            setentry(True)
            setbutton("Add", True)
        Else
            If vnamaitem.Text = String.Empty And vgroupitem.SelectedValue.ToString = "" Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Exit Sub
                End If
            End If
            ExecItem("insert")
            btnRefreshItem.PerformClick()
        End If
    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub

    Private Sub btnEditItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditItem.Click
        If btnEditItem.Text = btnEditItem.Tag Then
            'btnRefreshItem.PerformClick()
            setentry(True)
            setbutton("Edit", True)
        Else
            If viditem.Text = String.Empty And vgroupitem.SelectedValue.ToString = "" Then
                If MessageBox.Show("Maaf Data Entry Tidak Valid !!! " & vbCrLf & "Apakah Akan Diulangi ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    Exit Sub
                End If
            End If
            ExecItem("update")
            btnRefreshItem.PerformClick()
        End If
    End Sub

    Private Sub btnDelItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelItem.Click
        If viditem.Text = String.Empty Then
            MessageBox.Show("Silahkan Pilih Data Pada Grid/List !!!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        Else

            If MessageBox.Show("Apakah Anda Yakin Akan Menghapus Data ? ", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                ExecItem("delete")
                btnRefreshItem.PerformClick()
            End If
        End If
    End Sub
End Class